package com.xtourms.client.rm.ticket;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.Context;
import com.xtourms.client.ui.util.LocationWidget;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.TicketField;
import com.xtourms.shared.field.WorkerField;

public class TicketDetailManager extends Composite {

	private static TicketDetailManagerUiBinder uiBinder = GWT
			.create(TicketDetailManagerUiBinder.class);

	interface TicketDetailManagerUiBinder extends
			UiBinder<Widget, TicketDetailManager> {
	}

	private JSONObject ticketObj;
	private ContentFrame parent;
	private AsyncCallback<String> callback;
	
	public TicketDetailManager(ContentFrame parent, AsyncCallback<String> callback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.ticketObj = new JSONObject();
		this.parent = parent;
		this.callback = callback;
		initWidget();
	}
	
	public TicketDetailManager(JSONObject ticketObj, ContentFrame parent, AsyncCallback<String> callback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.ticketObj = ticketObj;
		this.parent = parent;
		this.callback = callback;
		initWidget();
		initTicket();
	}

	@UiField TextBox ticket_name;
	@UiField LocationWidget location;
	@UiField TextArea ticket_add;
	@UiField TextArea ticket_des;
	
	@UiField TextBox manager_name;
	@UiField TextBox manager_mobile;
	@UiField TextBox linkman_name;
	@UiField TextBox linkman_mobile;
	@UiField TextBox linkman_qq;
	
	@UiField ListBox payModel;
	@UiField TextBox unpay_money;
	@UiField TextBox unpay_days;
	@UiField TextBox bank_holder;
	@UiField TextBox bank_add;
	@UiField TextBox bank_account;
	
	@UiField ListBox symbiosis;
	@UiField DateBox start_date;
	@UiField DateBox end_date;
	
	private void initTicket(){
		ticket_name.setValue(((JSONString)ticketObj.get(TicketField.TICKET_NAME)).stringValue());
		String pN = ((JSONString)ticketObj.get(TicketField.TICKET_PROVINCE_NAME)).stringValue();
		String cN = ((JSONString)ticketObj.get(TicketField.TICKET_CITY_NAME)).stringValue();
		location.setLocation(pN, cN);
		ticket_add.setValue(((JSONString)ticketObj.get(TicketField.TICKET_ADD)).stringValue());
		ticket_des.setValue(((JSONString)ticketObj.get(TicketField.TICKET_DES)).stringValue());
		manager_name.setValue(((JSONString)ticketObj.get(TicketField.TICKET_MANAGER_NAME)).stringValue());
		manager_mobile.setValue(((JSONString)ticketObj.get(TicketField.TICKET_MANAGER_MOBILE)).stringValue());
		linkman_name.setValue(((JSONString)ticketObj.get(TicketField.TICKET_LINKMAN_NAME)).stringValue());
		linkman_mobile.setValue(((JSONString)ticketObj.get(TicketField.TICKET_LINKMAN_MOBILE)).stringValue());
		linkman_qq.setValue(((JSONString)ticketObj.get(TicketField.TICKET_LINKMAN_QQ)).stringValue());
		
		String model = ((JSONString)ticketObj.get(TicketField.TICKET_PAY_MODEL)).stringValue();
		int index = Util.findIndex(TicketContent.PAY_MODEL_STRS, model);
		payModel.setSelectedIndex(index);
		if(0 == index){
			unpay_money.setEnabled(false);
			unpay_days.setEnabled(false);
		}else{
			unpay_money.setValue(((JSONString)ticketObj.get(TicketField.TICKET_UNPAY_MONEY)).stringValue());
			unpay_days.setValue(((JSONString)ticketObj.get(TicketField.TICKET_UNPAY_DAY)).stringValue());
		}
		bank_holder.setValue(((JSONString)ticketObj.get(TicketField.TICKET_BANK_HOLDER)).stringValue());
		bank_add.setValue(((JSONString)ticketObj.get(TicketField.TICKET_BANK_ADD)).stringValue());
		bank_account.setValue(((JSONString)ticketObj.get(TicketField.TICKET_BANK_ACCOUNT)).stringValue());
		
		String symb = ((JSONString)ticketObj.get(TicketField.TICKET_SYMBIOSIS)).stringValue();
		index = Util.findIndex(TicketContent.SYMBIOSIS_STRS, symb);
		symbiosis.setSelectedIndex(index);
		start_date.setValue(java.sql.Date.valueOf(((JSONString)ticketObj.get(TicketField.TICKET_START_DATE)).stringValue()));
		end_date.setValue(java.sql.Date.valueOf(((JSONString)ticketObj.get(TicketField.TICKET_END_DATE)).stringValue()));
	}
	
	private void initWidget(){
		manager_mobile.setMaxLength(11);
		linkman_mobile.setMaxLength(11);
		linkman_qq.setMaxLength(20);
		start_date.setFormat(new MysqlDateFormat());
		end_date.setFormat(new MysqlDateFormat());
		//初始化合作关系
		Util.fillListBox(symbiosis, TicketContent.SYMBIOSIS_STRS);
		//初始化支付模式
		Util.fillListBox(payModel, TicketContent.PAY_MODEL_STRS);
		unpay_money.setEnabled(false);
		unpay_days.setEnabled(false);
		payModel.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				if(0 == payModel.getSelectedIndex()){
					unpay_money.setEnabled(false);
					unpay_days.setEnabled(false);
				} else {
					unpay_money.setEnabled(true);
					unpay_days.setEnabled(true);
				}
			}
		});
	}
	
	@UiHandler("confirm")
	void handleConfirm(ClickEvent evt){
		String hName = ticket_name.getText();
		ticketObj.put(TicketField.TICKET_NAME, new JSONString(hName));	
		int cId = location.getSelectedCityId();
		ticketObj.put(TicketField.TICKET_CITY_ID, new JSONNumber(cId));
		String hAdd = ticket_add.getText();
		ticketObj.put(TicketField.TICKET_ADD, new JSONString(hAdd));
		String hDes = ticket_des.getText();
		ticketObj.put(TicketField.TICKET_DES, new JSONString(hDes));
		
		String mName = manager_name.getText();
		ticketObj.put(TicketField.TICKET_MANAGER_NAME, new JSONString(mName));
		String mMobile = manager_mobile.getText();
		ticketObj.put(TicketField.TICKET_MANAGER_MOBILE, new JSONString(mMobile));
		String lName = linkman_name.getText();
		ticketObj.put(TicketField.TICKET_LINKMAN_NAME, new JSONString(lName));
		String lMobile = linkman_mobile.getText();
		ticketObj.put(TicketField.TICKET_LINKMAN_MOBILE, new JSONString(lMobile));
		String lQq = linkman_qq.getText();
		ticketObj.put(TicketField.TICKET_LINKMAN_QQ, new JSONString(lQq));
		
		int selectedIndex = payModel.getSelectedIndex();
		String pModel = TicketContent.PAY_MODEL_STRS[selectedIndex];
		ticketObj.put(TicketField.TICKET_PAY_MODEL, new JSONString(pModel));
		String upm = unpay_money.getText();
		ticketObj.put(TicketField.TICKET_UNPAY_MONEY, new JSONString(upm));
		String upd = unpay_days.getText();
		ticketObj.put(TicketField.TICKET_UNPAY_DAY, new JSONString(upd));
		String bHolder = bank_holder.getText();
		ticketObj.put(TicketField.TICKET_BANK_HOLDER, new JSONString(bHolder));
		String bAdd = bank_add.getText();
		ticketObj.put(TicketField.TICKET_BANK_ADD, new JSONString(bAdd));
		String bAccount = bank_account.getText();
		ticketObj.put(TicketField.TICKET_BANK_ACCOUNT, new JSONString(bAccount));
		
		selectedIndex = symbiosis.getSelectedIndex();
		String s = TicketContent.SYMBIOSIS_STRS[selectedIndex];
		ticketObj.put(TicketField.TICKET_SYMBIOSIS, new JSONString(s));
		
		Date sDate = start_date.getValue();
		ticketObj.put(TicketField.TICKET_START_DATE, new JSONString(sDate.toString()));
		Date eDate = end_date.getValue();
		ticketObj.put(TicketField.TICKET_END_DATE, new JSONString(eDate.toString()));
		
		ticketObj.put(TicketField.LASTMODIFY_WORKER, Context.ins.worker.get(WorkerField.WORKER_ID));
		
		TicketManagerFrame.tmsa.cOrMTicket(ticketObj.toString(), callback);
	}
	
	@UiHandler("back")
	void handleCancel(ClickEvent evt){
		parent.resumeContent();
	}
}
